package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.coupon.parameter.ConversionCouponBean;
import com.ymatou.iapi.coupon.parameter.GetConversionBatchBean;
import com.ymatou.iapi.coupon.service.GetConversionBatchCall;
import com.ymatou.iapi.couponmanage.parameter.CreateBagBean;
import com.ymatou.iapi.couponmanage.parameter.CreateBean;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.parameter.executeTaskBean;
import com.ymatou.iapi.couponmanage.service.CreateBagCall;
import com.ymatou.iapi.couponmanage.service.CreateCall;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymatou.iapi.couponmanage.service.executeTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponconversion;
import com.ymttest.database.model.couponconversioncode;
import com.ymttest.database.sqlwapper.couponconversionWapper;
import com.ymttest.database.sqlwapper.couponconversioncodeWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
//import com.ymttest.database.model.YmtCouponConversionBill;
//import com.ymttest.database.model.YmtCouponConversionCode;
//import com.ymttest.database.sqlwapper.YmtCouponConversionBillWapper;
//import com.ymttest.database.sqlwapper.YmtCouponConversionCodeWapper;
/**
*根据兑换码获取批次编号
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetConversionBatch{
	private static GetConversionBatchBean getconversionbatchBean;
	private static GetConversionBatchCall getconversionbatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据兑换码获取批次编号");
	}

	@Before
	public void caseUp() {
		getconversionbatchBean=new GetConversionBatchBean();
		getconversionbatchCall=new GetConversionBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getconversionbatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConversionBatch_001() {
		Logger.start(true,"根据兑换码获取批次编号");
		try {
			//生成批次及兑换码
			CouponServiceMCallServiceV2 couponMCallService = new CouponServiceMCallServiceV2();
			CouponServiceCallServiceV2 CouponServiceCallServiceV2 = new CouponServiceCallServiceV2();

			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);
			
			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = couponMCallService.create(createBean);
		
			// 等待兑换码生成
//			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
//					.selectByBillName(createBean.getBillName()).get(0);
//			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
//			
//			int flag=0;
//			while(ymtCouponConversionCodes.size()<createBean.getGenerateQuantity() && flag<500)
//			{
//				flag++;
//				Thread.sleep(200);
//				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
//			}
//			
//			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			couponconversion bill = new couponconversionWapper()
					.selectByconversionName(createBean.getBillName()).get(0);
			List<couponconversioncode> CouponConversionCodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
			
			getconversionbatchBean.setConversionCode(CouponConversionCodes.get(0).getConversionCode());
			getconversionbatchCall.setData(getconversionbatchBean);
			getconversionbatchCall.callService();			
			     
			 // 验证返回
			JsonObject result = getconversionbatchCall.getJsonObject().get("data").getAsJsonObject();

	        Logger.verifyEquals("\""+createCouponCall.getBatchCode()+"\"", result.get("batchCodes").getAsJsonArray().get(0).toString(),
	                "验证返回batchCodes");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConversionBatch_004() {
		Logger.start(true,"根据兑换码获取批次编号-套券");
		try {
			//生成批次及兑换码
			CouponServiceMCallServiceV2 couponMCallService = new CouponServiceMCallServiceV2();

			CreateBagBean createBagBean = couponMCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			createBagBean.setAcquireMethod(2);//2：领取方式是套券
			CreateBagCall createBagCall = couponMCallService.createPlatformCouponBag(createBagBean);
			
			List<String> bagCodes = new ArrayList<>();
			bagCodes.add(createBagCall.getbagCode());

			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(bagCodes);
			createBean.setBatchType(2);//2：套券
			CreateCall createCall = couponMCallService.create(createBean);
		
			//调job生成兑换码
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			couponconversion bill = new couponconversionWapper()
					.selectByconversionName(createBean.getBillName()).get(0);
			List<couponconversioncode> CouponConversionCodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
			getconversionbatchBean.setConversionCode(CouponConversionCodes.get(0).getConversionCode());
			getconversionbatchCall.setData(getconversionbatchBean);
			getconversionbatchCall.callService();			
			     
			 // 验证返回
			JsonObject result = getconversionbatchCall.getJsonObject().get("data").getAsJsonObject();
	        Logger.verifyEquals("\""+createBagCall.getbagCode()+"\"", result.get("batchCodes").getAsJsonArray().get(0).toString(),
	                "验证返回batchCodes");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConversionBatch_002() {
		Logger.start(true,"根据兑换码获取批次编号-ConversionCode空");
		try {
			
			getconversionbatchCall.setData(getconversionbatchBean);
			getconversionbatchCall.callService();			
			
			 // 验证返回
	        Logger.verifyIsContains("conversionCode:conversioncode is not blank", getconversionbatchCall.getJsonObject().get("message").getAsString(),
	                "验证返回message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConversionBatch_003() {
		Logger.start(true,"根据兑换码获取批次编号-ConversionCode不存在");
		try {
			getconversionbatchBean.setConversionCode("9999999999");
			getconversionbatchCall.setData(getconversionbatchBean);
			getconversionbatchCall.callService();			
			
			 // 验证返回
			JsonObject result = getconversionbatchCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(0, result.get("batchCodes").getAsJsonArray().size(),
	                "验证返回batchCodes");
	        Logger.verifyIsContains("操作成功！", getconversionbatchCall.getJsonObject().get("message").getAsString(),
	                "验证返回message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}